package org.cunamas.core.dao.impl;

import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.cunamas.core.bean.CommitteesInputBean;
import org.cunamas.core.dao.ComitesGestionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;


@Component
public class ComitesGestionDaoJdbcImpl implements ComitesGestionDao {
	
	private JdbcTemplate jdbcTemplate;
	
	private final static String SQL_DELETE = "delete from dbo.CM_COMITES where ANIO = ? and TRIMESTRE = ? ";
	
	private final static String SQL_INSERT = "insert into dbo.CM_COMITES(ANIO,TRIMESTRE,GRUPOSEDE,CODUBIGEO,DEP,PRO,DIS," +
			"CODCOMGES,NOMCOMGES,JUNTASDIRECTIVASVIG,CONCONVENIOSVIG,REALIZANMONITOREO,RECONOCIDOSXCALIDADSERVICIO,GESEFICIENTEMENTESERVICIOS,FECCREA) " +
										  "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	
	@Autowired
	public void setJdbcTemplate(DataSource dataSource) {
		 this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void persistComitesGestion(List<CommitteesInputBean> committeesInputBeanList){
		if(committeesInputBeanList==null) return;
		
		for (CommitteesInputBean committeesInputBean : committeesInputBeanList) {

			jdbcTemplate.update(SQL_INSERT, 
					new Object[] { committeesInputBean.getAnio(),committeesInputBean.getTrimestre(), 
					committeesInputBean.getGrupoSede(),
					committeesInputBean.getCodigoUbigeo(), committeesInputBean.getDepartamento(),
					committeesInputBean.getProvincia(), committeesInputBean.getDistrito(),
					committeesInputBean.getCodigoComiteGestion(), committeesInputBean.getNombreComiteGestion(),
					committeesInputBean.getJuntasDirectivasVigentes(), committeesInputBean.getConConveniosVigentes(),
					committeesInputBean.getRealizanMonitoreo(), committeesInputBean.getReconocidosPorCalidadServicio(),
					committeesInputBean.getGestionaEficientementeServicios(),
					new Date()
					});
			
		}

	}
	
	
	public void deleteComitesGestionByAnioAndTrimestre(Long anio, Long trimestre){
		jdbcTemplate.update(SQL_DELETE, new Object[] {anio, trimestre});
	}

}
